using System;
using System.ComponentModel;

namespace Fornax020.Devices
{
    /// <summary>
    /// X射线设备状态类
    /// 统一管理设备的所有状态信息
    /// </summary>
    public class XRayDeviceState : INotifyPropertyChanged
    {
        #region 私有字段
        private double _currentKv = 0.0;
        private double _currentMa = 0.0;
        private double _feedbackKv = 0.0;
        private double _feedbackMa = 0.0;
        private double _feedbackFil = 0.0;
        private string _firmwareVersion = "";
        private string _lastError = "";
        private bool _isConnected = false;
        private bool _isExposing = false;
        private DateTime _lastUpdateTime = DateTime.Now;
        private string _lastCommandType = "";
        #endregion

        #region 公共属性
        /// <summary>
        /// 当前设置的KV值
        /// </summary>
        public double CurrentKv
        {
            get => _currentKv;
            set
            {
                if (_currentKv != value)
                {
                    _currentKv = value;
                    OnPropertyChanged(nameof(CurrentKv));
                    OnPropertyChanged(nameof(IsKvChanged));
                }
            }
        }

        /// <summary>
        /// 当前设置的MA值
        /// </summary>
        public double CurrentMa
        {
            get => _currentMa;
            set
            {
                if (_currentMa != value)
                {
                    _currentMa = value;
                    OnPropertyChanged(nameof(CurrentMa));
                    OnPropertyChanged(nameof(IsMaChanged));
                }
            }
        }

        /// <summary>
        /// 设备反馈的KV值
        /// </summary>
        public double FeedbackKv
        {
            get => _feedbackKv;
            set
            {
                if (_feedbackKv != value)
                {
                    _feedbackKv = value;
                    OnPropertyChanged(nameof(FeedbackKv));
                    OnPropertyChanged(nameof(IsKvChanged));
                }
            }
        }

        /// <summary>
        /// 设备反馈的MA值
        /// </summary>
        public double FeedbackMa
        {
            get => _feedbackMa;
            set
            {
                if (_feedbackMa != value)
                {
                    _feedbackMa = value;
                    OnPropertyChanged(nameof(FeedbackMa));
                    OnPropertyChanged(nameof(IsMaChanged));
                }
            }
        }

        /// <summary>
        /// 设备反馈的FIL值
        /// </summary>
        public double FeedbackFil
        {
            get => _feedbackFil;
            set
            {
                if (_feedbackFil != value)
                {
                    _feedbackFil = value;
                    OnPropertyChanged(nameof(FeedbackFil));
                }
            }
        }

        /// <summary>
        /// 固件版本
        /// </summary>
        public string FirmwareVersion
        {
            get => _firmwareVersion;
            set
            {
                if (_firmwareVersion != value)
                {
                    _firmwareVersion = value;
                    OnPropertyChanged(nameof(FirmwareVersion));
                }
            }
        }

        /// <summary>
        /// 最后的错误信息
        /// </summary>
        public string LastError
        {
            get => _lastError;
            set
            {
                if (_lastError != value)
                {
                    _lastError = value;
                    OnPropertyChanged(nameof(LastError));
                    OnPropertyChanged(nameof(HasError));
                }
            }
        }

        /// <summary>
        /// 是否已连接
        /// </summary>
        public bool IsConnected
        {
            get => _isConnected;
            set
            {
                if (_isConnected != value)
                {
                    _isConnected = value;
                    OnPropertyChanged(nameof(IsConnected));
                    OnPropertyChanged(nameof(ConnectionStatus));
                }
            }
        }

        /// <summary>
        /// 是否正在曝光
        /// </summary>
        public bool IsExposing
        {
            get => _isExposing;
            set
            {
                if (_isExposing != value)
                {
                    _isExposing = value;
                    OnPropertyChanged(nameof(IsExposing));
                    OnPropertyChanged(nameof(ExposureStatus));
                }
            }
        }

        /// <summary>
        /// 最后更新时间
        /// </summary>
        public DateTime LastUpdateTime
        {
            get => _lastUpdateTime;
            set
            {
                if (_lastUpdateTime != value)
                {
                    _lastUpdateTime = value;
                    OnPropertyChanged(nameof(LastUpdateTime));
                }
            }
        }

        /// <summary>
        /// 最后处理的命令类型
        /// </summary>
        public string LastCommandType
        {
            get => _lastCommandType;
            set
            {
                if (_lastCommandType != value)
                {
                    _lastCommandType = value;
                    OnPropertyChanged(nameof(LastCommandType));
                }
            }
        }
        #endregion

        #region 计算属性
        /// <summary>
        /// KV值是否发生变化
        /// </summary>
        public bool IsKvChanged => Math.Abs(CurrentKv - FeedbackKv) > 0.1;

        /// <summary>
        /// MA值是否发生变化
        /// </summary>
        public bool IsMaChanged => Math.Abs(CurrentMa - FeedbackMa) > 0.1;

        /// <summary>
        /// 是否有错误
        /// </summary>
        public bool HasError => !string.IsNullOrEmpty(LastError);

        /// <summary>
        /// 连接状态描述
        /// </summary>
        public string ConnectionStatus => IsConnected ? "已连接" : "未连接";

        /// <summary>
        /// 曝光状态描述
        /// </summary>
        public string ExposureStatus => IsExposing ? "正在曝光" : "未曝光";

        /// <summary>
        /// 设备状态摘要 - 简化版本，不在标题栏显示详细信息
        /// </summary>
        public string StatusSummary => "";
        #endregion

        #region 方法
        /// <summary>
        /// 重置所有状态
        /// </summary>
        public void Reset()
        {
            CurrentKv = 0.0;
            CurrentMa = 0.0;
            FeedbackKv = 0.0;
            FeedbackMa = 0.0;
            FeedbackFil = 0.0;
            FirmwareVersion = "";
            LastError = "";
            IsConnected = false;
            IsExposing = false;
            LastCommandType = "";
            LastUpdateTime = DateTime.Now;
        }

        /// <summary>
        /// 更新状态时间戳
        /// </summary>
        public void UpdateTimestamp()
        {
            LastUpdateTime = DateTime.Now;
        }

        /// <summary>
        /// 清除错误
        /// </summary>
        public void ClearError()
        {
            LastError = "";
        }

        /// <summary>
        /// 检查状态是否有效
        /// </summary>
        /// <returns>状态是否有效</returns>
        public bool IsValid()
        {
            return IsConnected && !HasError;
        }

        /// <summary>
        /// 获取状态快照
        /// </summary>
        /// <returns>状态快照</returns>
        public XRayDeviceStateSnapshot GetSnapshot()
        {
            return new XRayDeviceStateSnapshot
            {
                CurrentKv = CurrentKv,
                CurrentMa = CurrentMa,
                FeedbackKv = FeedbackKv,
                FeedbackMa = FeedbackMa,
                FeedbackFil = FeedbackFil,
                FirmwareVersion = FirmwareVersion,
                LastError = LastError,
                IsConnected = IsConnected,
                IsExposing = IsExposing,
                LastUpdateTime = LastUpdateTime,
                LastCommandType = LastCommandType
            };
        }

        /// <summary>
        /// 从快照恢复状态
        /// </summary>
        /// <param name="snapshot">状态快照</param>
        public void RestoreFromSnapshot(XRayDeviceStateSnapshot snapshot)
        {
            if (snapshot == null) return;

            CurrentKv = snapshot.CurrentKv;
            CurrentMa = snapshot.CurrentMa;
            FeedbackKv = snapshot.FeedbackKv;
            FeedbackMa = snapshot.FeedbackMa;
            FeedbackFil = snapshot.FeedbackFil;
            FirmwareVersion = snapshot.FirmwareVersion;
            LastError = snapshot.LastError;
            IsConnected = snapshot.IsConnected;
            IsExposing = snapshot.IsExposing;
            LastUpdateTime = snapshot.LastUpdateTime;
            LastCommandType = snapshot.LastCommandType;
        }
        #endregion

        #region INotifyPropertyChanged实现
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

    /// <summary>
    /// 设备状态快照类
    /// 用于保存和恢复设备状态
    /// </summary>
    public class XRayDeviceStateSnapshot
    {
        public double CurrentKv { get; set; }
        public double CurrentMa { get; set; }
        public double FeedbackKv { get; set; }
        public double FeedbackMa { get; set; }
        public double FeedbackFil { get; set; }
        public string FirmwareVersion { get; set; }
        public string LastError { get; set; }
        public bool IsConnected { get; set; }
        public bool IsExposing { get; set; }
        public DateTime LastUpdateTime { get; set; }
        public string LastCommandType { get; set; }
    }
}
